using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Scene;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
namespace MicroMsg.Plugin.LBS.Scene
{
	public class NetSceneLbs : NetSceneBaseEx<LbsRequest, LbsResponse, LbsRequest.Builder>
	{
		private const string TAG = "NetSceneLbs";
		public const uint MM_LBS_FIND = 1u;
		public const uint MM_LBS_REMOVE = 2u;
		public const uint MM_LBS_FINDGG = 3u;
		public const uint MM_LBS_FINDMM = 4u;
		private const int ACCURACY_LIMIT = 200;
		public const string nLocationOutdate = "nLOcationOutdate";
		public bool doSceneFind(double latitude, double longitude, uint findType)
		{
			if (latitude > 90.0 || latitude < -90.0)
			{
				Log.smethod_1("NetSceneLbs", "invalid args , latitude = " + latitude);
				return false;
			}
			if (longitude <= 180.0 && longitude >= -180.0)
			{
				ConstantsProtocol.LBS_INFO = string.Concat(new object[]
				{
					"_",
					(int)latitude,
					".",
					(int)longitude
				});
				uint opCode = 1u;
				if (3u == findType)
				{
					opCode = 3u;
				}
				else
				{
					if (4u == findType)
					{
						opCode = 4u;
					}
				}
				this.doSceneEx(opCode, longitude, latitude, 200, LbsPosition.getMacXml(), LbsPosition.getCellXml());
				return true;
			}
			Log.smethod_1("NetSceneLbs", "invalid args , longitude = " + longitude);
			return false;
		}
		public void doSceneRemove()
		{
			this.doSceneEx(2u, 0.0, 0.0, 0, "", "");
		}
		private void doSceneEx(uint opCode, double longitude, double latitude, int precision, string macAddr, string cellId)
		{
			Log.smethod_5("NetSceneLbs", string.Concat(new object[]
			{
				"lbs scene begin, latitude =  ",
				latitude,
				", longitude = ",
				longitude
			}));
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(31);
			this.mBuilder.OpCode = opCode;
			this.mBuilder.Latitude = (float)latitude;
			this.mBuilder.Longitude = (float)longitude;
			this.mBuilder.Precision = precision;
			this.mBuilder.MacAddr = macAddr;
			this.mBuilder.CellId = cellId;
			this.mSessionPack.mConnectMode = 2;
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/lbsfind";
			base.endBuilder();
		}
		private void saveLocationOutDate(int outDateTime)
		{
			ConfigMgr.setInt("nLOcationOutdate", outDateTime);
			LBS.getInstance().refreshFootPrintFlag();
		}
		protected override void onSuccess(LbsRequest request, LbsResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			uint opCode = request.OpCode;
			if (opCode == 2u)
			{
				if (ret == RetConst.MM_OK)
				{
					Log.smethod_5("NetSceneLbs", "lbs remove success.");
					this.saveLocationOutDate(0);
					EventCenter.postEvent(EventConst.ON_LBS_REMOVE_SUCCESS, null, null);
					return;
				}
				Log.smethod_1("NetSceneLbs", "lbs remove failed,ret = " + ret);
				EventCenter.postEvent(EventConst.ON_LBS_REMOVE_ERROR, ret, null);
				return;
			}
			else
			{
				if (ret != RetConst.MM_OK)
				{
					Log.smethod_5("NetSceneLbs", "lbs find failed, ret = " + ret);
					EventCenter.postEvent(EventConst.ON_LBS_FIND_ERROR, ret, null);
					return;
				}
				Log.smethod_5("NetSceneLbs", "lbs find success, contact count = " + response.ContactCount);
				this.saveLocationOutDate((int)(response.FlushTime + Util.getNowSeconds()));
				List<LbsPeople> param = this.procLbsContactInfoList(response.ContactListList);
				EventCenter.postEvent(EventConst.ON_LBS_FIND_SUCCESS, param, null);
				return;
			}
		}
		public static bool isLbsLocationOutdate()
		{
			bool flag = false;
			uint @int = (uint)ConfigMgr.getInt("nLOcationOutdate", 0);
			if (@int == 0u)
			{
				flag = true;
			}
			else
			{
				if ((uint)Util.getNowSeconds() > @int)
				{
					flag = true;
				}
			}
			Log.smethod_5("NetSceneLbs", "isLbsLocationOutdate :" + flag);
			return flag;
		}
		private List<LbsPeople> procLbsContactInfoList(IList<LbsContactInfo> ContactListList)
		{
			List<LbsPeople> list = new List<LbsPeople>();
			if (ContactListList.get_Count() > 0)
			{
				using (IEnumerator<LbsContactInfo> enumerator = ContactListList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						LbsContactInfo current = enumerator.get_Current();
						LbsPeople lbsPeople = new LbsPeople();
						if (current.Province != null)
						{
							lbsPeople.strArea = ToolFunction.GetRegionShow(current.Province, current.City);
						}
						if (current.Signature != null)
						{
							lbsPeople.strSignature = current.Signature;
						}
						if (current.Distance != null)
						{
							lbsPeople.strDistance = current.Distance;
						}
						if (current.NickName != null)
						{
							lbsPeople.strNickName = current.NickName;
						}
						if (current.UserName != null)
						{
							lbsPeople.strUserName = current.UserName;
						}
						lbsPeople.strAlias = Util.nullAsNil(current.Alias);
						lbsPeople.nSex = current.Sex;
						lbsPeople.bHasImg = (current.ImgStatus != 4u);
						lbsPeople.nHeadImgVersion = current.HeadImgVersion;
						lbsPeople.nVerifyFlag = current.VerifyFlag;
						lbsPeople.strVerifyInfo = current.VerifyInfo;
						lbsPeople.nSnsFlag = current.SnsUserInfo.SnsFlag;
						lbsPeople.strSnsBGImgID = current.SnsUserInfo.SnsBGImgID;
						lbsPeople.nSnsBGObjectID = current.SnsUserInfo.SnsBGObjectID;
						lbsPeople.strMyBrandList = current.MyBrandList;
						lbsPeople.nBrandFlag = current.CustomizedInfo.BrandFlag;
						lbsPeople.strBrandExternalInfo = current.CustomizedInfo.ExternalInfo;
						lbsPeople.strBrandInfo = current.CustomizedInfo.BrandInfo;
						lbsPeople.strBrandIconURL = current.CustomizedInfo.BrandIconURL;
						list.Add(lbsPeople);
					}
				}
			}
			return list;
		}
		protected override void onFailed(LbsRequest request, LbsResponse response)
		{
			uint opCode = request.OpCode;
			if (opCode == 1u)
			{
				Log.smethod_1("NetSceneLbs", "lbs find failed in client side. ");
				EventCenter.postEvent(EventConst.ON_LBS_FIND_ERROR, -800000, null);
				return;
			}
			if (opCode == 2u)
			{
				Log.smethod_1("NetSceneLbs", "lbs removed failed in client side. ");
				EventCenter.postEvent(EventConst.ON_LBS_REMOVE_ERROR, -800000, null);
			}
		}
	}
}
